var express = require('express');
var request = require('request');
var session = require('express-session')
var cookieParser = require('cookie-parser');
var querystring = require('querystring')
var router = express.Router();
var passport = require('passport')
var registercontroller = require('../controllers/registercontroller')
// router.get('/register', function(req, res, next) {
//     res.render('register');
//   });
//注册
router.post('/register', function (req, res, next) {
    registercontroller.register(req, res)
})

//登录
router.post('/login', function (req, res, next) {
    registercontroller.login(req, res)
})
//忘记密码
router.put('/forpwd', passport.authenticate('jwt', { session: false }), function (req, res, next) {
    registercontroller.forpwd(req, res)
})

router.get('/getVCode', function (req, res, next) {
    // console.log('>>>>>>>>>>>>>>>>>>>>>');
    // registercontroller.yzm(req, res)
    console.log('>>>>>>>>>>>>>>>>>>>>>');
    console.log(req.query)
    var tel = req.query.telephone
      var code = ''
      for (var i = 0; i < 6; i++) {
          code += Math.floor(Math.random() * 10);
      }
      console.log(code)
    //   req.session.TelvCode = []
    //   req.session.TelvCode.push({ telephone: tel, TelvCode: code })
    //   console.log(req.session.TelvCode)
    //   console.log('保存到session里面的手机号' + req.session.TelvCode[0].telephone)
    //   console.log('保存到session里面的手机号验证码' + req.session.TelvCode[0].vCode)
      console.log('后台发出去的验证码：' + code)
  
    // var queryData = querystring.stringify({
    //   "mobile": tel,  // 接受短信的用户手机号码
    //   "tpl_id": "188832",  // 您申请的短信模板ID，根据实际情况修改
    //   "tpl_value": "#code#=" + code,  // 您设置的模板变量，根据实际情况修改
    //   "key": "9e15d635ed68cd4ccc8b3a916979c47d",  // 应用APPKEY(应用详细页查询)
    // });
  
    // var queryUrl = 'http://v.juhe.cn/sms/send?' + queryData;
    // console.log('queryUrl:' + queryUrl)
    // request(queryUrl, function (error, response, body) {
    //   if(error) throw error;
    //   if (!error && response.statusCode == 200) {
    
    //     var jsonObj = JSON.parse(body); // 解析接口返回的JSON内容
    //     res.json({data:jsonObj.error_code})
    //     console.log(jsonObj.error_code)
    //   } else {
    //     res.json('请求异常');
    //   }
    // })
    res.json({code:200,msg:"验证码发送成功",data:code})
    })


module.exports = router;